#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_HttpDownloader.h"
#include <QNetworkReply>
#include <QElapsedTimer>

#include "selfdefine.h"

class QNetworkAccessManager;
class QFile;

class HttpDownloader : public QMainWindow
{
    Q_OBJECT

private slots:
    void on_pushButton_upload_clicked();
    void on_pushButton_download_clicked();
    void on_toolButton_upload_clicked();
    void on_toolButton_download_clicked();
    void on_pushButton_cancel_clicked();

    void OnFinished(QNetworkReply* reply);
    void OnUploadReadContent();
    void OnError(QNetworkReply::NetworkError code);
    void OnProgress(qint64 bytesSent, qint64 bytesTotal);
    void OnDownloadData();
    void OnDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);

public:
    explicit HttpDownloader(QWidget *parent = Q_NULLPTR);
protected:
    void OnInitDialog();
private:
    void WriteLog(const QString& log);
    void InitManager();
    void InitFile();
    void SetWidgetState(WIDGET_STATE state);
    void InitProgressbar();
    QString GetFileName(const QString &);
    void SetCurBytes(qint64 bytes);
private:
    Ui::HttpDownloaderClass ui;

    QFile* m_file;
    QNetworkAccessManager* m_manager;
    QNetworkReply* m_reply;
    WIDGET_STATE m_widget_state;
    QElapsedTimer m_elapsed;
};
